﻿using B.S.RbacData.Domain.Traceabilitys;
using B.S.RbacData.ErrorCode;
using B.S.RbacData.Infrastructure;
using B.S.RbacData.Write.API.Applications.Commands.Traceabilitys;
using MediatR;

namespace B.S.RbacData.Write.API.Applications.Commandhandlters.Traceabilitys
{
    /// <summary>
    /// 批量删除产品信息命令处理
    /// </summary>
    public class RangerIsDelCommandHandlter : IRequestHandler<RangerIsDelCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<ProductModel> productRep;

        public RangerIsDelCommandHandlter(IBaseRepository<ProductModel> productRep)
        {
            this.productRep = productRep;
        }


        public async Task<ApiResult<int>> Handle(RangerIsDelCommand request, CancellationToken cancellationToken)
        {

            //根据产品编号查询要删除的产品信息
            var productlist = productRep.GetAll().Where(x => !x.IsDeleted && request.ProductIds.Contains(x.ID)).ToList();

            foreach (var item in productlist)
            {
                item.DelTime = DateTime.Now;
                item.IsDeleted = true;
            }
            //操作逻辑删除
            var result = await productRep.UpdateRanger(productlist);


            return new ApiResult<int>
            {
                code = result>0? ResultEnumCode.Ok : ResultEnumCode.fails,
                data = result,
                message = result > 0 ? "删除成功" : "删除失败",
               
            };
        }
    }
}
